package cn.zhku.edu.productservice.service.impl;

import cn.zhku.edu.productservice.mapper.BrandMapper;
import cn.zhku.edu.productservice.mapper.CategoryMapper;
import cn.zhku.edu.productservice.mapper.ProductMapper;
import cn.zhku.edu.feign.pojo.bo.ParentProduct;
import cn.zhku.edu.feign.pojo.entry.Brand;
import cn.zhku.edu.feign.pojo.entry.Category;
import cn.zhku.edu.feign.pojo.entry.Product;
import cn.zhku.edu.feign.pojo.vo.DetailProductVO;
import cn.zhku.edu.feign.pojo.vo.GoodsItemVO;
import cn.zhku.edu.productservice.service.ProductServies;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiesImpl extends ServiceImpl<ProductMapper, Product> implements ProductServies {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public int deleteByPrimaryKey(Integer gid) {
        return productMapper.deleteByPrimaryKey(gid);
    }

    @Override
    public int insert(Product record) {
        return productMapper.insert(record);
    }

    @Override
    public int insertSelective(Product record) {
        return productMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Product record) {
        return productMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Product record) {
        return productMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ParentProduct> selectAllFilter(String[] filter, int tid) {
        return productMapper.selectAllFilter(filter,tid);
    }

    @Override
    public List<GoodsItemVO> getIndexRecommendGoodsLists(int[] recomendedGoodsGids) {
        List<GoodsItemVO> result=new ArrayList<>();
        Product p;
        for (int gid : recomendedGoodsGids) {
            p=getById(gid);
            result.add(new GoodsItemVO(p.getGid(),p.getGoodsThumbnailImg(),p.getGoodsName(),p.getGoodsPrice()));
        }
        return result;
    }

    @Override
    public DetailProductVO getDetailProductVO(Integer gid) {
        Product product=getById(gid);
        Brand brand= brandMapper.selectByPrimaryKey(product.getBid());
        Category category=categoryMapper.selectByPrimaryKey(product.getTid());
        String[] specifications;
        if(product.getSpecifications()!=null){
            specifications=product.getSpecifications().split("\r\n");
        }else {
            specifications= new String[]{};
        }
        return new DetailProductVO(product.getGid(),category.getTypeName(),product.getGoodsName(),product.getGoodsPrice(), brand.getBrandName(), product.getGoodsDetail(),product.getSales(),product.getGoodsDetailImg(),specifications);
    }


}
